/**
 * created by 江心才子, 2019/8/10
 * Copyright (c) 2019, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.mine.userinfo

import androidx.databinding.ObservableField
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okgo.request.PostRequest
import com.lzy.okrx2.adapter.ObservableBody
import com.wyl.base.ACacheHelper
import com.wyl.base.COMMON_PIC_UPDATE
import com.wyl.base.MINE_USER_INFO_UPDATE
import com.wyl.base.bean.UserData
import com.wyl.base.userData
import com.wyl.libbase.base.BaseViewModel
import java.io.File

/**
 * 项目名称：ShortVideo
 * 创建人：江心才子
 * 创建时间：2019-08-10 15:19
 * 内容描述：
 * 修改说明：
 */
class UserInfoViewModel : BaseViewModel() {
    var mUserData = ObservableField<UserData>()

    fun updateInfo(
        name: String? = null,
        sex: String? = null,
        phone: String? = null
    ) {
        val request: PostRequest<BaseResponse<*>> = OkGo.post<BaseResponse<*>>(MINE_USER_INFO_UPDATE)
            .params("id", ACacheHelper.id)

        name?.let { request.params("name", name) }
        sex?.let { request.params("sex", sex) }
        phone?.let { request.params("phone", phone) }

        request.converter(object : JsonConvert<BaseResponse<*>>() {})
            .adapt(ObservableBody<BaseResponse<*>>())
            .doOnSubscribe { loading.value = true }
            .doFinally { loading.value = false }
            .subscribe(
                { data ->
                    name?.let {
                        userData?.name = name
                    }
                    sex?.let {
                        userData?.sex = sex
                    }
                    phone?.let {
                        userData?.phone = phone
                    }
                    success.value = data.msg
                }, {
                    onError(it)
                }
            ).toDisposables(disposables)
    }

    fun updatePic(file: File) {
        var url = ""
        OkGo.post<BaseResponse<List<Map<String, String>>>>(COMMON_PIC_UPDATE)
            .params("files", file)
            .converter(object : JsonConvert<BaseResponse<List<Map<String, String>>>>() {})
            .adapt(ObservableBody<BaseResponse<List<Map<String, String>>>>())
            .doOnSubscribe { loading.value = true }
            .doFinally { loading.value = false }
            .map { it.data }
            .flatMap {
                url = it[0]["url"] ?: ""
                OkGo.post<BaseResponse<*>>(MINE_USER_INFO_UPDATE)
                    .params("id", ACacheHelper.id)
                    .params("avatar", url)
                    .converter(object : JsonConvert<BaseResponse<*>>() {})
                    .adapt(ObservableBody<BaseResponse<*>>())
            }
            .subscribe(
                {
                    userData?.avatar = url
                    success.value = "修改成功"
                }, {
                    onError(it)
                }
            ).toDisposables(disposables)
    }

    fun getUserInfo() {

    }


}